<?php

namespace app\api\controller\xiservice;

use app\common\controller\xiservice\XiserviceApi;
use app\common\model\xiservice\Message as MessageModel;
use app\common\model\xiservice\Messageread;
use app\common\model\xiservice\Service;
use think\Db;

/**
 * 消息模块
 */
class Message extends XiserviceApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    /**
     * @ApiTitle (消息列表)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.message/list)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams  (name="type", type="integer", description="消息类型:0=平台通知,1=新订单通知,2=订单取消通知,3=订单评价通知,4=接单通知,5=服务通知")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="返回数据")
     */
    public function list()
    {

        $type = $this->request->request('type', 0);
        if ($type == 0) {
            $list = MessageModel::where('type', 1)
                ->order('createtime desc')
                ->field('id,title,content,createtime')
                ->paginate();
        } else {
            $list = MessageModel::with(['order' => function ($query) {
                $query->withField('order_no,confirm_time');
            }, 'service' => function ($query) {
                $query->withField('name,image');
            }, 'user' => function ($query) {
                $query->withField('username,mobile,avatar');
            }])
                ->where('type', 2)
                ->where('message.order_status', $type)
                ->where('to_user_id', $this->auth->id)
                ->order('createtime desc')
                ->paginate();
        }
        $this->success('查询成功', $list);
    }


    /**
     * @ApiTitle (消息列表)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.message/lastest_msg)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams  (name="type", type="integer", description="消息类型:0=平台通知,1=新订单通知,2=订单取消通知,3=订单评价通知,4=接单通知,5=服务通知")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="返回数据")
     */
    public function lastest_msg()
    {
        $type = $this->request->request('type', 0);
        if ($type == 0) {
            $data = MessageModel::where('type', 1)
                ->order('createtime desc')
                ->field('id,title,content,createtime')
                ->find();
            $msgIds = MessageModel::where('type', 1)->column('id');
            $rmsgIds = Messageread::where('user_id', $this->auth->id)->column('message_id');
            $array_diff = array_diff($msgIds, $rmsgIds);
            $unread = count($array_diff);
        } else {
            $data = MessageModel::with(['order' => function ($query) {
                $query->withField('order_no,confirm_time');
            }, 'service' => function ($query) {
                $query->withField('name,image');
            }, 'user' => function ($query) {
                $query->withField('username,mobile,avatar');
            }])
                ->where('type', 2)
                ->where('message.order_status', $type)
                ->where('to_user_id', $this->auth->id)
                ->order('createtime desc')
                ->find();
            //未读订单消息
            $msgIds = MessageModel::where('type', 2)->where('order_status', $type)->where('to_user_id', $this->auth->id)->column('id');
            if ($msgIds) {
                $rmsgIds = Messageread::where('user_id', $this->auth->id)->column('message_id');
                if ($rmsgIds) {
                    $array_diff = array_diff($msgIds, $rmsgIds);
                    $unread = count($array_diff);
                } else {
                    $unread = count($msgIds);
                }
            } else {
                $unread = 0;
            }


        }
        $res = [
            "message" => $data,
            "unread" => $unread
        ];
        $this->success('查询成功', $res);
    }


    /**
     * @ApiTitle (获取未读消息数量)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.message/get_unread)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams  (name="type", type="integer", description="消息类型:0=平台通知,1=新订单通知,2=订单取消通知,3=订单评价通知,4=接单通知,5=服务通知")
     * @ApiReturnParams   (name="code", type="integer",  sample="0")
     * @ApiReturnParams   (name="msg", type="string",  sample="返回成功")
     * @ApiReturnParams   (name="data", type="integer", sample="0", description="返回数据")
     */
    public function get_unread()
    {
        //未读系统消息
        $msgIds = MessageModel::where('type', 1)->column('id');
        $rmsgIds = Messageread::where('user_id', $this->auth->id)->column('message_id');
        $array_diff = array_diff($msgIds, $rmsgIds);
        //未读订单消息
        $msgIds2 = MessageModel::where('type', 2)->where('to_user_id', $this->auth->id)->column('id');
        $array_diff2 = array_diff($msgIds2, $rmsgIds);
        $alldiff = array_merge($array_diff, $array_diff2);

        $this->success('查询成功', count($alldiff));
    }

    /**
     * @ApiTitle (消息已读设置)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.message/set_read)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams  (name="type", type="integer", description="消息类型:0=平台通知,1=新订单通知,2=订单取消通知,3=订单评价通知,4=接单通知,5=服务通知")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="返回数据")
     */
    public function set_read()
    {

        $type = $this->request->request('type', 0);
        if ($type == 0) {
            //type
            $msgIds = MessageModel::where('type', 1)->column('id');
            $rmsgIds = Messageread::where('user_id', $this->auth->id)->column('message_id');
            $array_diff = array_diff($msgIds, $rmsgIds);

        } else {
            //to_user_id
            $msgIds = MessageModel::where('type', 2)->where('order_status', $type)->where('to_user_id', $this->auth->id)->column('id');
            $rmsgIds = Messageread::where('user_id', $this->auth->id)->column('message_id');
            $array_diff = array_diff($msgIds, $rmsgIds);

        }

        if ($array_diff) {
            $data = [];
            foreach ($array_diff as $key => $value) {
                $data[] = [
                    'user_id' => $this->auth->id,
                    'message_id' => $value,
                    'createtime' => time()
                ];
            }
            $messagemodel = new Messageread();
            $res = $messagemodel->saveAll($data);
        }
        $this->success('更新成功', []);
    }

}